package org.example.demo20240504.mq;

import cn.hutool.json.JSONUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.demo20240504.feign.DemoFeign;
import org.example.demo20240504.mq.dto.OrderDTO;
import org.example.demo20240504.service.DemoService;
import org.springframework.stereotype.Component;

import javax.validation.constraints.Null;
import java.util.function.Consumer;

/**
 * @ClassName DemoInput
 * @Description TODO
 * @Author huarujin
 * @Date 2024/5/4 14:21
 * @Version 1.0
 */

@Component
@Slf4j
@RequiredArgsConstructor
public class DemoInput implements Consumer<OrderDTO> {

    private final DemoService demoService;
    private final DemoFeign demoFeign;

    @Override
    public void accept(OrderDTO orderDTO) {
        int status = 1;
        try {
            log.info("接收到的消息 {}", JSONUtil.toJsonStr(orderDTO));
            // 扣件库存
            demoService.subOrder(orderDTO);
            // 其他业务
            // 。。。
            // 模拟异常
            throw new NullPointerException("模拟空指针异常");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("扣件库存发生异常 {}", e.getMessage());
            status = 0;
        } finally {
            // 商品扣减完成 通知 producer
            demoFeign.finish(orderDTO.getId(), status);
        }
    }
}
